<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="formData" :rules="rules" label-width="120px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formData.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

            <!-- 工号 入职时间 -->

            <el-col :span="8">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="formData.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="formData.mobile"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <select-tree v-model="formData.departmentId" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="formData.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formData.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formData.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>

            <!-- 员工照片 -->

            <el-col :span="8">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload :value="formData.staffPhoto" @input="formData.staffPhoto=$event" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="btnOk">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import selectTree from '@/views/employee/components/select-tree'
import { addEmpApi, editEmpApi, getEmpInfoApi } from '@/api/imployee'
// import { addEmpApi, editEmpApi, getEmpInfoApi } from '@/api/imployee'
import imageUpload from '@/views/employee/components/image-upload'
export default {
  components: {
    selectTree, imageUpload
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: '', // 聘用形式
        departmentId: '', // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: ''
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, {
          min: 1, max: 4, message: '姓名为1-4位'
        }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
        //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!this.formData.correctionTime) callback()
              new Date(this.formData.correctionTime) < new Date(value)
                ? callback(new Error('转正时间不能小于入职时间')) : callback()
            }
          }
        ],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!this.formData.timeOfEntry) callback()
              new Date(this.formData.timeOfEntry) > new Date(value)
                ? callback(new Error('转正时间不能小于入职时间')) : callback()
            }
          }]
      }
    }
  },
  created() {
    // 判断是否有动态路由，如果有则说明是修改，获取数据 回显
    if (this.$route.params && this.$route.params.id) this.getDetailData(this.$route.params.id)
  },
  methods: {
    // 提交逻辑
    async btnOk() {
      // 表单效验
      await this.$refs.userForm.validate()
      // 加工处理
      // 发送请求
      this.formData.id ? await editEmpApi(this.formData) : await addEmpApi(this.formData)
      // 提示成功
      this.$message.success('操作成功')
      // 返回
      this.$router.back()
    },
    // 数据回显
    async getDetailData(id) {
      const res = await getEmpInfoApi(id)
      this.formData = res
    }
  }

}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>
